package com.app.controller;

import com.app.common.Result;
import com.app.model.AdminRoleModel;
import com.app.model.AdminUserModel;
import com.app.service.AdminRoleService;
import com.app.service.AdminUserService;
import com.app.utils.CommonHelp;
import com.app.utils.TokenUtils;
import com.app.vo.AdminRoleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/adminRole")
public class AdminRoleController {
    @Autowired
    private AdminRoleService adminRoleService;

    @Autowired
    private AdminUserService adminUserService;

    //查询当前登录人的角色信息
    @GetMapping("/getAdminRole")
    public Result<Object> getAdminRole(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminId = TokenUtils.verify(token);
        if (adminId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel adminUser = adminUserService.getById(Integer.valueOf(adminId));
        if (adminUser == null) {
            return Result.error(405, "用户不存在");
        }

        AdminRoleModel adminRole = adminRoleService.getById(adminUser.getRoleId());
        List<String> roleModelList = new ArrayList<>();
        if (adminRole.getRules() != null) {
            String[] roleArr = adminRole.getRules().split(",");//获取字符串
            if (roleArr != null) {
                for (int i = 0; i < roleArr.length; i++) {
                    roleModelList.add(roleArr[i]);
                }
                adminRole.setPermissionList(roleModelList);
            }
        }
        return Result.success(adminRole);
    }

    //根据id查询详情
    @GetMapping("/getRoleById")
    public Result getRoleById(Integer id, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminId = TokenUtils.verify(token);
        if (adminId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel adminUser = adminUserService.getById(Integer.valueOf(adminId));
        if (adminUser == null) {
            return Result.error(405, "用户不存在");
        }

        AdminRoleModel adminRole = adminRoleService.getById(id);
        List<String> roleModelList = new ArrayList<>();
        String[] roleArr = adminRole.getRules().split(",");//获取字符串
        if (roleArr != null) {
            for (int i = 0; i < roleArr.length; i++) {
                roleModelList.add(roleArr[i]);
            }
            adminRole.setPermissionList(roleModelList);
        }
        return Result.success(adminRole);
    }

    @GetMapping("/getRoleList")//角色管理分页查询
    public Result<Object> getRoleList(@RequestParam("page") int page,
                                      @RequestParam("limit") int limit,
                                      @RequestParam(required = false) String roleName, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminId = TokenUtils.verify(token);
        if (adminId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel adminUser = adminUserService.getById(Integer.valueOf(adminId));
        if (adminUser == null) {
            return Result.error(405, "用户不存在");
        }

        AdminRoleVo roleList = adminRoleService.getRoleList(page, limit, roleName);
        return Result.success(roleList);
    }

    // 新增和修改角色
    @PostMapping("/addAndUpdateRole")
    public Result addAndUpdateRole(@RequestBody AdminRoleModel adminRoleModel, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminId = TokenUtils.verify(token);
        if (adminId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel adminUser = adminUserService.getById(Integer.valueOf(adminId));
        if (adminUser == null) {
            return Result.error(405, "用户不存在");
        }
        AdminRoleModel adminRole = adminRoleService.getById(adminRoleModel.getId());
        if (adminRole != null) {
            adminRoleModel.setUpdateTime(CommonHelp.getNowTimestamp());
            adminRoleService.updateById(adminRoleModel);
            return Result.success("修改成功");
        } else {
            adminRoleModel.setCreateTime(CommonHelp.getNowTimestamp());
            adminRoleService.save(adminRoleModel);
            return Result.success("添加成功");
        }
    }

    //修改状态
    @GetMapping("/updateRoleStatus")
    public Result<Object> updateRoleStatus(@RequestParam("id") Integer id,
                                           @RequestParam("status") Integer status, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminId = TokenUtils.verify(token);
        if (adminId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.valueOf(adminId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        AdminRoleModel roleModel = new AdminRoleModel();
        roleModel.setId(id);
        roleModel.setStatus(status);
        adminRoleService.updateById(roleModel);
        return Result.success("操作成功");
    }

    //删除角色
    @GetMapping("/delRole")
    public Result<Object> delRole(Integer id, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminId = TokenUtils.verify(token);
        if (adminId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.valueOf(adminId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        adminRoleService.removeById(id);
        return Result.success("删除成功");
    }

    //获取角色下拉框
    @GetMapping("/getRoleByStatus")
    private Result<Object> getRoleByStatus() {
        return Result.success(adminRoleService.getRoleByStatus());
    }
}
